import pandas as pd
from flask import Flask, request, jsonify
from flask_cors import CORS
from ollama import chat, Client
from openai import OpenAI

with open('data.csv', 'r', encoding='utf-8') as file:
    # 一次性读取整个文件内容
    content = file.read()

app = Flask(__name__)
app.config['APPLICATION_ROOT'] = '/bc'
CORS(app)

client = Client(host='http://10.60.93.136:11434')
messages1 = [
    {'role': 'system', 'content': '你是一个数据分析师。'}
]
messages2 = [
    {'role': 'system', 'content': '你是一个猫娘。'}
]

@app.route('/', methods=['GET'])
def hello():
    return "hello world!"

@app.route('/data', methods=['GET'])
def show_data():
    try:
        return content

    except Exception as e:
        return f"<h2>nono</h2><p>{str(e)}</p>", 500

@app.route('/deepseek', methods=['GET'])
def deepseek():
    #f"帮我分析一下这个班级的学生水平\n{content}"
    user_input = request.args.get('input')
    # 添加用户消息到历史
    messages1.append({'role': 'user', 'content': user_input})

    # 获取模型回复
    response = client.chat(model='deepseek-r1:1.5b', messages=messages1)

    # 添加模型回复到历史
    messages1.append({'role': 'assistant', 'content': response['message']['content']})

    # 打印模型回复
    return(f"deepseek：{response['message']['content']}")


@app.route('/open', methods=['GET'])
def modescope():

    client = OpenAI(
        api_key="e1579e92-1838-48bf-b869-348a4bd1c214",  # 请替换成您的ModelScope SDK Token
        base_url="https://api-inference.modelscope.cn/v1/"
    )

    response = client.chat.completions.create(
        model="deepseek-ai/DeepSeek-V3",  # ModleScope Model-Id
        messages=[
            {
                'role': 'system',
                'content': '你是一个数据分析师。'
            },
            {
                'role': 'user',
                #f"{content}\n帮我分析一下这个班级的学生数据"
                'content':f"deepseek：{content}\n 帮我分析这个班级的数据"
            }
        ],
        stream=False
    )

    ans = response.choices[0].message.content
    return (f"deepseek：{ans}")

@app.route('/cat', methods=['GET'])
def cat():
    user_input = request.args.get('input')
    # 添加用户消息到历史
    messages2.append({'role': 'user', 'content': user_input})

    # 获取模型回复
    response = client.chat(model='cat_7b_q4_k_m:latest', messages=messages2)

    # 添加模型回复到历史
    messages2.append({'role': 'assistant', 'content': response['message']['content']})

    # 打印模型回复
    return(f"猫娘：{response['message']['content']}")

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0', port=5000)